﻿using Common;
using Entity;
using Police.App_Start;
using Police.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;

namespace Police.Controllers
{
    public class HomeController : Controller
    {
        private SysUserBLL bSysUser = new SysUserBLL();
        private SysUserRoleBLL bSysUserRole = new SysUserRoleBLL();
        private SysMenuBLL bSysMenu = new SysMenuBLL();

        [OwnActionFilter]
        public ActionResult Index()
        {
            SysUser user = bSysUser.Get("admin", "123456");

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        [HttpPost]
        public ActionResult LogOff()
        {
            //WebSecurity.Logout();

            return Json("1111");
        }

        [HttpPost]
        public ActionResult Login()
        {
            string username = Request.Form["username"];
            string password = Request.Form["password"];

            SysUser user = bSysUser.Get(username, password);

            var status = new { status = "FAIL", message = "用户名或密码不正确！" };

            if (user != null)
            {
                status = new { status = "SUCCESS", message = "正确！" };

                List<SysUserRole> userRoleList = bSysUserRole.Get(user.Id);

                string ids = string.Empty;
                foreach(SysUserRole temp in userRoleList)
                {
                    ids += string.Format("{0},", temp.RoleId);
                }
                ids = ids.Substring(0, ids.Length - 1);

                List<SysMenu> menus = bSysMenu.Get(ids);

                UserSession userSession = new UserSession();
                userSession.User = user;
                userSession.Menus = menus;
                Session["userInfo"] = userSession;
            }

            return Json(status);
        }

        public ActionResult Default()
        {
            return View();
        }

        [HttpGet]
        public ActionResult OutLookMenu()
        {
            UserSession userInfo = Session["userInfo"] as UserSession;
            List<SysMenu> sysMenus = bSysMenu.Get(userInfo.User.Id);

            List<Object> treeNodes = new List<Object>();
		    foreach (SysMenu sysMenu in sysMenus) 
            {
			    if (sysMenu.ParentId.Equals("0")) 
                {
				    TreeRootNode treeRootNode = new TreeRootNode();
				    treeRootNode.id = sysMenu.MenuId.ToString();
				    treeRootNode.text = sysMenu.MenuName;
				    treeNodes.Add(treeRootNode);
			    } 
                else 
                {
				    TreeNode treeNode = new TreeNode();
				    treeNode.id = sysMenu.MenuId.ToString();
				    treeNode.text = sysMenu.MenuName;
				    treeNode.pid = sysMenu.ParentId.ToString();
				    treeNode.iconCls = sysMenu.MenuIcon;
				    treeNode.url = sysMenu.MenuUrl;
				    treeNode.iconPosition = "top";
                    treeNodes.Add(treeNode);
			    }
		    }

            return Json(treeNodes, JsonRequestBehavior.AllowGet);
        }



    }
}
